<%@page import="java.sql.DriverManager"%>
<%@page import="Configurations.MySQL"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%
    session.setAttribute("displayPage", "oldReviews");
    // Redirect if this page is being displayed by itself
    if (request.getServletPath().equals("/oldReviews.jsp")) {
        System.out.println("Current Page is set to oldReviews. Redirect to index.jsp");
        response.sendRedirect("");
    }

    String applicationEmail = (String) session.getAttribute("appRequest");
    String username = (String) session.getAttribute("username");
    String message = "You are currently assigned this application.";
    boolean assigned = true;

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    Class.forName(MySQL.DB_DRIVER);
    con = DriverManager.getConnection(MySQL.DB_URL, MySQL.DB_USER, MySQL.DB_PASS);
    // Get the user's list of assigned applications to check if this application
    // is assigned to them
    String sql = "SELECT * FROM " + MySQL.DB_LOGIN + " WHERE " + MySQL.DB_LOGIN_USERNAME + "=?";
    ps = con.prepareStatement(sql);
    ps.setString(1, username);
    System.out.println(ps.toString());
    rs = ps.executeQuery();
    if (rs.next()) {
        String assignedApps = rs.getString(MySQL.DB_LOGIN_ASSIGNED);
        if (assignedApps.indexOf(applicationEmail) < 0) {
            message = "You are NOT currently assigned this application.";
            assigned = false;
        }
    }

    if (assigned == false) {
        sql = "SELECT * FROM " + MySQL.DB_REVS + " WHERE " + MySQL.DB_REVS_APP_EMAIL + "=?";
        ps = con.prepareStatement(sql);
        ps.setString(1, applicationEmail);
    } else {
        sql = "SELECT * FROM " + MySQL.DB_REVS + " WHERE " + MySQL.DB_REVS_APP_EMAIL + "=? AND " + MySQL.DB_REVS_REV_USER + "=?";
        ps = con.prepareStatement(sql);
        ps.setString(1, applicationEmail);
        ps.setString(2, username);
    }
    System.out.println(ps.toString());
    rs = ps.executeQuery();
%>
<h3>Previous Reviews</h3>
<div id="content">
    <b>Please Note: </b> If you are currently assigned to review this 
    application you will only be able to see previous reviews that you have 
    authored. The names of the reviewers are also hidden from everyone except 
    the chair.<br/><br/>
    <b>Old Reviews Application With Email: </b><%=applicationEmail%><br/><br/>
    <b>Your Status: </b><%=message%><br/><br/>
    <a href="application.jsp">Go Back to The Application</a>
</div><br/>
<div id="content">
    <table border="1">
        <tr>
            <td>#</td>
            <td><b><% if ((Boolean) session.getAttribute("isChair")) {
                    out.println("Reviewer Username");
                }%><b/></td>
            <td><b>Rating</b></td>
            <td width="75%"><b>Comments</b></td>
        </tr>
        <%
            int counter = 1;
            while (rs.next()) {
        %>
        <tr>
            <td><%=counter%></td>
            <td><% if ((Boolean) session.getAttribute("isChair")) {
                    out.println(rs.getString(MySQL.DB_REVS_REV_USER));
                }%></td>
            <td><%=rs.getString(MySQL.DB_REVS_RATING)%></td>
            <td><%=rs.getString(MySQL.DB_REVS_COMMENTS)%></td>
        </tr>
        <%  counter++;
            }
            con.close();
            ps.close();
            rs.close();
        %>
    </table>
</div><br/>